#!/bin/bash
# Description: This shell script used to generate patch file
#      Author: Zhang.Kang <13874854275@163.com>
#     Version: 1.0.0  

PROJ_PATH=`pwd`

PACK_PATH=${PROJ_PATH}/tarballs
PATCH_PATH=${PROJ_PATH}/patches

BOARD=K6
PATCH_SUFFIX=${BOARD}.patch

function do_clean()
{
    #if [ -f .config ] ; then
        #set -x
        #cp .config .cfg-${BOARD}
        #set +x
    #fi

    #echo "WARNNING: Maybe need input sudo passwd here: "
    bash build.sh clean
}

if [ $# != 1 ] ; then
    echo "Usage: $0 [src_path]"
    printf "\nExample: \n"
    echo "$0 linux-3.2.0"

    exit ;
fi


SRC=`basename $1`

if [ ! -d ${SRC} ] ; then
    printf "\nERROR: ${SRC} source code not exist, exit now\n\n"
    exit
fi

if [ ! -f ${PACK_PATH}/${SRC}.tar.gz ] ; then
    printf "\nERROR: ${SRC}.tar.gz packet not exist, exit now\n\n"
    exit
fi

cd ${PROJ_PATH}

cd ${SRC}
   make clean > /dev/null 2>&1
cd -

# rename new source code
mv ${SRC} ${SRC}-${BOARD}

# decompress orignal soruce code packet
tar -xzf ${PACK_PATH}/${SRC}.tar.gz

# generate patch file 
diff -Nuar ${SRC} ${SRC}-${BOARD}  > ${SRC}-${PATCH_SUFFIX}

# remove orignal soruce code
rm -rf ${SRC}

# recover new source code
mv ${SRC}-${BOARD} ${SRC} 

